package br.com.winesys.geta.persistence.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCConnectionFactory {

	private String driver;
	private String url;
	private Properties properties;
	private boolean sharedConnection;
	private Connection connection;

	JDBCConnectionFactory(Properties properties, boolean sharedConnection) {
		super();
		this.properties = properties;
		this.driver = (String) this.properties.get("driver");
		this.url = (String) this.properties.get("url");
		this.sharedConnection = sharedConnection;
	}

	public Connection getConnection() throws SQLException, ClassNotFoundException {
		if (!this.sharedConnection || this.connection == null || this.connection.isClosed()) {
			Class.forName(this.driver);
			this.connection = DriverManager.getConnection(this.url, this.properties);
		}
		return this.connection;
	}
}
